package com.voodoo.lib_frame.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import androidx.annotation.NonNull;

/**
 * Author: voodoo
 * CreateDate: 2021/05/16
 * Description: 屏幕工具类
 */
public class ScreenUtils {

    /**
     * 获取状态栏高度
     *
     * @return 状态栏高度
     */
    public static int getStatusBarHeight(@NonNull Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resourceId);
    }

    /**
     * 设置沉浸式状态栏
     *
     * @param activity {@link Activity}
     */
    public static void setImmersiveStatusBar(@NonNull Activity activity) {
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // >= 5.0
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else { // 否则的话 API<=4.4
            WindowManager.LayoutParams winParams = window.getAttributes();
            int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            window.setAttributes(winParams);
        }
    }

    /**
     * 是否需要隐藏软键盘
     *
     * @param view  点击的View
     * @param event Event
     * @return 是否隐藏软键盘
     */
    public static boolean isShouldHideInput(View view, MotionEvent event) {
        if (view instanceof EditText) { // 如果View不为空并且View是EditText类
            int[] leftTop = {0, 0};
            // 获取输入框当前位置的左上角坐标
            view.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + view.getHeight();
            int right = left + view.getWidth();
            if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，不隐藏软键盘
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

}
